package ubi.com.wifitest;

import android.app.Dialog;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.List;

/**
 * wifi管理器的dialog
 *
 * @author eli chang
 */
public class WifiManagerDialog extends Dialog implements View.OnClickListener, WifiAdmin.OnWifiResult {

    private static final String TAG = "WifiManagerDialog";

    public Context context;
    //连接按钮
    private TextView textConnect;
    //取消按钮
    private TextView textCancelConnect;
    //wifi视图
    private WifiView wifiView;
    //wifi管理器
    private WifiAdmin wifiAdmin;

    public WifiManagerDialog(Context context) {
        super(context, R.style.dialog_wifi_waring);
        this.context = context;
        //初始化wifi管理器，设置wifi状态接口
        wifiAdmin = new WifiAdmin(context);
        wifiAdmin.setOnScanWifiResult(this);

        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        setContentView(R.layout.dialog_wifi_connect);
        getWindow().setWindowAnimations(R.style.animation_dialog_wifi);
        setCanceledOnTouchOutside(true);

        //初始化三个视图
        textConnect = findViewById(R.id.connect_dialog_connect);
        textCancelConnect = findViewById(R.id.connect_dialog_cancel);
        textConnect.setOnClickListener(this);
        textCancelConnect.setOnClickListener(this);
        wifiView = findViewById(R.id.connect_dialog_wifi);
        wifiView.setZOrderOnTop(true);
    }

    @Override
    public void show() {
        super.show();
        //设置窗口尺寸
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = Util.dip2px(context, 240);
        layoutParams.height = Util.dip2px(context, 240);
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);

        //初始化wifi状态
        if (wifiAdmin != null) {
            if (!wifiAdmin.isWifiEnable()) {
                //wifi未开启
                wifiView.setState(WifiView.STATE_WIFI_DISABLE);
                setText(textConnect, "启动");
            } else if (!wifiAdmin.isConnectWithAP()) {
                //未连接到设备
                wifiView.setState(WifiView.STATE_DEVICE_DISCONNECTED);
                setText(textConnect, "连接");
            } else {
                //已经连接到设备
                wifiView.setState(WifiView.STATE_DEVICE_CONNECTED);
                setText(textConnect, "连接");
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.connect_dialog_connect:
                if (wifiAdmin != null) {
                    if (!wifiAdmin.isWifiEnable()) {
                        //开启wifi设备
                        wifiAdmin.enableWifiDevice();
                    } else if (!wifiAdmin.isConnectWithAP()) {
                        //开始搜索设备
                        wifiAdmin.startScan();
                        wifiView.setState(WifiView.STATE_WIFI_SCANNING);
                    }
                }
                break;

            case R.id.connect_dialog_cancel:
                dismiss();
                break;

            default:
                break;
        }
    }

    /**
     * 搜索到附近wifi设备
     *
     * @param values
     */
    @Override
    public void onGetScanResult(List<ScanResult> values) {
        //停止扫描
        if (wifiAdmin != null) {
            wifiAdmin.stopScan();
        }
        //遍历列表，寻找设备
        ScanResult result = null;
        for (ScanResult r : values) {
            if (r.SSID.equals(WifiAdmin.SSID)) {
                result = r;
                break;
            }
        }
        if (result != null) {
            //搜做到设备后，尝试连接
            wifiView.setState(WifiView.STATE_DEVICE_CONNECTING);

            WifiConfiguration configuration = new WifiConfiguration();
            configuration.BSSID = result.BSSID;
            configuration.SSID = "\"" + WifiAdmin.SSID + "\"";
            configuration.status = WifiConfiguration.Status.ENABLED;
            configuration.preSharedKey = "\"" + WifiAdmin.PWD + "\"";
            wifiAdmin.connectToAP(configuration);
        } else {
            //未搜索到设备
            wifiView.setState(WifiView.STATE_DEVICE_404);
        }
    }

    /**
     * 设备已连接的接口
     */
    @Override
    public void onConnectWithAP() {
        if (isShowing()) {
            //设置wifi视图状态为以连接
            wifiView.setState(WifiView.STATE_DEVICE_CONNECTED);
            setText(textConnect, "连接");
        }
    }

    /**
     * 设备已断开的接口
     */
    @Override
    public void onDisconnectWithAP() {
        if (!isShowing()) {
            //当dialog未显示，显示会话框
            initView();
            show();
        } else {
            //dialog显示时，更新状态
            if (wifiAdmin.isWifiEnable()) {
                wifiView.setState(WifiView.STATE_DEVICE_DISCONNECTED);
                setText(textConnect, "连接");
            } else {
                wifiView.setState(WifiView.STATE_WIFI_DISABLE);
                setText(textConnect, "启动");
            }
        }
    }

    /**
     * 设置文字
     *
     * @param textView
     * @param text
     */
    private void setText(TextView textView, String text) {
        if (textView != null) {
            textView.setText(text);
        }
    }

    @Override
    public void dismiss() {
        if (wifiAdmin != null) {
            wifiAdmin.stopScan();
        }
        super.dismiss();
    }

    public void destroy() {
        if (wifiAdmin != null) {
            wifiAdmin.stopReceiver();
            wifiAdmin.stopScan();
            wifiAdmin = null;
        }
        if (wifiView != null) {
            wifiView.destroy();
            wifiView = null;
        }
    }
}
